var addcion = '<div class="layui-form-item"><div class="layui-inline"><label class="layui-form-label">货币类型</label><div class="layui-input-inline" style="width: 200px;"><select lay-ignore class="layui-input coin_id coin-type" lay-filter="aihao"><option value=""></option></select></div><div class="layui-form-mid">比例</div><div class="layui-input-inline" style="width: 200px;"><input type="text" autocomplete="off" class="layui-input config"><span class="layui-input-bq">%</span></div><div class="layui-input-inline" style="width: 38px;"><a class="layui-btn layui-bnt-sc layui-btn-tr" onclick="delCion($(this))"  href="javascript:;"><i class="layui-icon">&#xe640;</i></a></div></div></div>'
//添加货币配置
function addCion() {
	$("#coinForm").append(addcion);
	$("#coinForm .layui-form-item:last-child").find("select.coin-type").html($("#cionHtml").html());
}

//删除货币配置
function delCion($this) {
	$this.parents(".layui-form-item").remove();
}

//根据货币ID匹配货币名称
function cinName(id) {
	var coinName;
	$.each($.parseJSON(coin), function(index, value) {
		if(value.id == id) {
			coinName = value.coin_name
		}
	});
	return coinName
}

//获取当前货币配置
function GainCoin() {
	$.ajax({
		type: 'post',
		dataType: 'json',
		url: coinpzSrc,
		data: 'bonus=jiaquan&config=coin',
		success: function(data) {
			var data = data.config_value;
			$.each(data, function(index, value) {
				$("#coinForm").append('<div class="layui-form-item"><div class="layui-inline"><label class="layui-form-label">货币类型</label><div class="layui-input-inline" style="width: 200px;"><select lay-ignore class="layui-input coin-type coin_id" lay-filter="aihao"><option value="' + value.coin_id + '">' + cinName(value.coin_id) + '</option></select></div><div class="layui-form-mid">比例</div><div class="layui-input-inline" style="width: 200px;"><input type="text" value="' + value.config + '" autocomplete="off" class="layui-input config"><span class="layui-input-bq">%</span></div><div class="layui-input-inline" style="width: 38px;"><a class="layui-btn layui-bnt-sc layui-btn-tr" onclick="delCion($(this))"  href="javascript:;"><i class="layui-icon">&#xe640;</i></a></div></div></div>');
			})

		}
	});
}
//根据等级ID匹配等级名称
function levelName(id) {
	var levelName;
	$.each($.parseJSON(level), function(index, value) {
		if(value.id == id) {
			levelName = value.level_name
		}
	});
	return levelName
}

//获取当前浮动值配置
function GainFloat(){
	$.ajax({
		type: 'post',
		dataType: 'json',
		url: coinpzSrc,
		data: 'bonus=jiaquan&config=float',
		success: function(data) {
			var top=data.config_value.ftop;
			var bnt=data.config_value.fbnt;
			$("#floatForm input.ftop").val(top);
			$("#floatForm input.fbnt").val(bnt);
		}
	});
}
//获取当前会员等级加权分红配置
function Gainlevel(){
	$.ajax({
		type: 'post',
		dataType: 'json',
		url: coinpzSrc,
		data: 'bonus=jiaquan&config=levels',
		success: function(data) {
		   var levelsHtml=''; 
		   $.each(data.config_value, function(i,v) {
		   	   levelsHtml+='<div class="layui-form-item" data-level="'+v.level_id+'"><div class="layui-inline"><label class="layui-form-label">'+levelName(v.level_id)+'</label><div class="layui-input-inline" style="width: 200px;"><input type="text" value="'+v.rate+'" autocomplete="off" class="layui-input rate"><span class="layui-input-bq">%</span></div><div class="layui-form-mid">倍数</div><div class="layui-input-inline" style="width: 200px;"><input type="text" value="'+v.multiple+'" autocomplete="off" class="layui-input multiple"></div></div></div>'
		   });
		   $("#levleForm").append(levelsHtml);
		}
	});
}
layui.use('layer', function() {
	var $ = layui.jquery,layer = layui.layer;
	var active = {
		//保存货币配置
		SubmitCoin: function(){
			var Array = [];
			$("#coinForm .layui-form-item").each(function(index){
				var coin_id = $(this).find("select.coin_id option:selected").val();
				var config = $(this).find("input.config").val();
				var data = {
					coin_id : coin_id,
					config : config
				};
				Array.push(data);
			});
			$.ajax({
				type:'post',
				dataType:'json',
				url:SubmitCoinSrc,
				data:{
					token:token,
					bonus_name:"jiaquan",
					config_name:"coin",
					data:Array
				},
				success:function(data){
		              var code=data.code;
		              var msg=data.msg;
				      if(code == 1) {
							layer.alert(msg, {
								icon: 1
							}, function() {
								location.reload();
							})
						} else {
							layer.alert(msg, {
								icon: 5
							})
						}
				}
			});
		},
		SubmitFloat:function(){
			var ftop = $("#floatForm input.ftop").val();
			var fbnt = $("#floatForm input.fbnt").val();
			var data = {
					ftop : ftop,
					fbnt : fbnt
			};
			$.ajax({
				type:'post',
				dataType:'json',
				url:SubmitCoinSrc,
				data:{
					token:token,
					bonus_name:"jiaquan",
					config_name:"float",
					data:data
				},
				success:function(data){
		              var code=data.code;
		              var msg=data.msg;
				      if(code == 1) {
							layer.alert(msg, {
								icon: 1
							}, function() {
								location.reload();
							})
						} else {
							layer.alert(msg, {
								icon: 5
							})
						}
				}
			});
			
			
		},
		Submitlevel:function(){
			var Array = [];
			$("#levleForm .layui-form-item").each(function(index){
				var id=$(this).attr("data-level");
				var rate = $(this).find("input.rate").val();
				var multiple = $(this).find("input.multiple").val();
				var data = {
					rate : rate,
					multiple : multiple,
					level_id : id
				};
				Array.push(data);
			});
			$.ajax({
				type:'post',
				dataType:'json',
				url:SubmitCoinSrc,
				data:{
					token:token,
					bonus_name:"jiaquan",
					config_name:"levels",
					data:Array
				},
				success:function(data){
		              var code=data.code;
		              var msg=data.msg;
				      if(code == 1) {
							layer.alert(msg, {
								icon: 1
							}, function() {
								location.reload();
							})
						} else {
							layer.alert(msg, {
								icon: 5
							})
						}
				}
			});
			
			
		}
		
	};
	$('.right-center').on('click', '.right-btn', function() {
		var othis = $(this),method = othis.data('method');
		active[method] ? active[method].call(this, othis) : '';
	});
	
	
	
})
$(function() {
	cionFun();
	GainCoin();
	GainFloat();
	Gainlevel();
})